//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Components;

namespace CommunityServer.Controls
{
    /// <summary>
    /// Enables thread subscription using a checkbox instead of a button
    /// </summary>
    public class ThreadSubscribeCheckBox : Control, ITrackedThread
    {

        #region members
        private bool _isTracked;
        private int _threadid;
        private string _resourceName, _text;
        CheckBox chk = null;
        CSContext csContext = CSContext.Current;
        #endregion

        protected override void CreateChildControls()
        {
            this.Controls.Clear();
            chk = new CheckBox();
            this.Controls.Add(chk);
        }

        public string ResourceName
        {
            get{return _resourceName;}
            set{_resourceName = value;}
        }

        public string Text
        {
            get{return _text;}
            set{_text = value;}
        }

        public bool IsTracked
        {
            get{return _isTracked;}
            set{_isTracked = value;}
        }

        public int ThreadID
        {
            get{return _threadid;}
            set{_threadid = value;}
        }

        protected override void OnInit(EventArgs e)
        {
            
            
            base.OnInit (e);
        }


        protected override void OnLoad(EventArgs e)
        {
            if(csContext.IsAuthenticated)
                AjaxManager.Register(this,"ThreadSubscribeCheckBox");
            else
                this.Visible = false;

            base.OnLoad (e);
        }

        private static readonly string script = "ThreadSubscribeCheckBox.CheckToggleEmailReplies('{0}',{1},null);";
        protected override void OnPreRender(EventArgs e)
        {
            if(csContext.IsAuthenticated)
            {
                if(Globals.IsNullorEmpty(this.Text) && !Globals.IsNullorEmpty(this.ResourceName))
                {
                    this.Text = ResourceManager.GetString(this.ResourceName);
                }

                chk.Text = this.Text;
                chk.Checked = this.IsTracked;
                chk.Attributes.Add("onclick",string.Format(script,this.ClientID,this.ThreadID));
            }

            base.OnPreRender (e);
        }




        [AjaxMethod(IncludeControlValuesWithCallBack=false)]
        public void CheckToggleEmailReplies(int threadID)
        {

            try
            {
                if(!csContext.User.IsAnonymous)
                {
                    ThreadTracking.ReverseThreadTrackingOptions(threadID, csContext.User.UserID);
                }
            }
            catch(Exception e)
            {
                new CSException(CSExceptionType.UnknownError, "Error in ThreadSubscribeLinkButton callback", e).Log();
                throw e;
            }
        }
    }
}